package com.polaris.live.popup

import android.content.Context
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.PopupUserSpaceMoreBinding
import com.polaris.live.dialog.ManageBanDialog
import com.polaris.live.dialog.ManageMuteDialog
import com.polaris.live.dialog.common.ConfirmCanalDetailDialog
import com.polaris.live.net.userService
import com.polaris.live.resp.go_resp.ManagerUnMuteRequest
import com.polaris.live.translation.R.string

/**
 * UserSpaceMorePopup   用户控件选择更多    这里的map传对应值好知道是否拉黑过或者关注过   如果没有则默认判断没有拉黑过
 *                      用map主要是方面未来扩展
 * @author Created by 半仙 on 2023/9/23/023 12:02
 */
class UserSpaceMorePopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val otherId: Long,
    private val mute: Boolean, //是否禁言
    private val ban: Boolean,  //是否封号
    private val map: Map<String, Boolean>,
    private val onClickCallback: (type: Int) -> Unit,
    private val onMuteCallback: (mute: Boolean) -> Unit,
    private val onBanCallback: (ban: Boolean) -> Unit,
) : BottomPopupView(context) {

    //根据返回值判断点击的那个
    companion object {

        const val REPORT = 1
        const val BLACK = 2
        const val FOLLOWE = 3

        const val blocak = "blocked"
        const val followe = "followed"
    }

    private lateinit var mBinding: PopupUserSpaceMoreBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_user_space_more
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupUserSpaceMoreBinding.bind(popupImplView)
        if (UserConst.isManage()) { //是管理员
            mBinding.manageLl.visibility = VISIBLE
            mBinding.cancelSpeaking.text = StringUtils.getString(
                if (mute) string.cancel_no_speaking else string.no_speaking
            )
            mBinding.cancelBan.text = StringUtils.getString(
                if (ban) string.cancel_the_ban else string.ban
            )
        }
        val blocak = map[blocak]
        if (blocak != null && blocak) {
            mBinding.moveBlackList.text = StringUtils.getString(string.move_out_black_list)
        }

        val fllowe = map[followe]
        if (fllowe != null && !fllowe) {
            mBinding.cancelFollower.text = StringUtils.getString(string.cliek_follower)
            mBinding.cancelFollower.visibility = GONE
            mBinding.followerLine.visibility = GONE
        } else {
            mBinding.cancelFollower.visibility = VISIBLE
            mBinding.followerLine.visibility = VISIBLE
        }

        mBinding.apply {
            report.setOnSingleClickListener {
                onClickCallback.invoke(REPORT)
                dismiss()

            }
            moveBlackList.setOnSingleClickListener {
                onClickCallback.invoke(BLACK)
                dismiss()
            }
            cancelFollower.setOnSingleClickListener {
                onClickCallback.invoke(FOLLOWE)
                dismiss()
            }
            cancel.setOnSingleClickListener {
                dismiss()
            }
            cancelSpeaking.setOnSingleClickListener { //禁言
                if (mute) {
                    ConfirmCanalDetailDialog.show(
                        context,
                        mTitle = StringUtils.getString(string.cancel_no_speaking),
                        mDetail = StringUtils.getString(string.no_speaking_tips),
                        mConfirm = StringUtils.getString(string.sure),
                        mCanal = StringUtils.getString(string.cancel),
                        onConfirmCallback = { unMuteRequest() },
                    )
                } else {
                    ManageMuteDialog.show(context, mViewModel, otherId) {
                        onMuteCallback.invoke(it)
                    }
                }
                dismiss()
            }
            cancelBan.setOnSingleClickListener { //封禁
                if (ban) {
                    ConfirmCanalDetailDialog.show(
                        context,
                        mTitle = StringUtils.getString(string.cancel_the_ban),
                        mDetail = StringUtils.getString(string.ban_tips),
                        mConfirm = StringUtils.getString(string.sure),
                        mCanal = StringUtils.getString(string.cancel),
                        onConfirmCallback = { unBanRequest() },
                    )

                } else {
                    ManageBanDialog.show(context, mViewModel, otherId, {}) {
                        onBanCallback.invoke(true)
                    }
                }

                dismiss()
            }
        }
    }

    /**
     * 取消禁言
     */
    private fun unMuteRequest() {
        val request = ManagerUnMuteRequest(otherId)
        mViewModel.requestLoading({ userService.managerUnMute(request) }, {
            ToastGravity.showGravity(StringUtils.getString(string.un_forbidden_successfully))
            onMuteCallback.invoke(false)
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    /**
     * 取消封禁
     */
    private fun unBanRequest() {
        mViewModel.requestLoading({ userService.managerUnBan(ManagerUnMuteRequest(otherId)) }, {
            ToastGravity.showGravity(StringUtils.getString(string.un_ban_successfully))
            onBanCallback.invoke(false)
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }
}